package com.ruoyix.weblog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.weblog.domain.bo.BlogResourceBo;
import com.ruoyix.weblog.domain.vo.BlogResourceVo;
import com.ruoyix.weblog.domain.BlogResource;
import com.ruoyix.weblog.mapper.BlogResourceMapper;
import com.ruoyix.weblog.service.IBlogResourceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 资源信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@RequiredArgsConstructor
@Service
public class BlogResourceServiceImpl implements IBlogResourceService {

    private final BlogResourceMapper baseMapper;

    /**
     * 查询资源信息
     */
    @Override
    public BlogResourceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询资源信息列表
     */
    @Override
    public TableDataInfo<BlogResourceVo> queryPageList(BlogResourceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogResource> lqw = buildQueryWrapper(bo);
        Page<BlogResourceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询资源信息列表
     */
    @Override
    public List<BlogResourceVo> queryList(BlogResourceBo bo) {
        LambdaQueryWrapper<BlogResource> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlogResource> buildQueryWrapper(BlogResourceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogResource> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, BlogResource::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), BlogResource::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getPath()), BlogResource::getPath, bo.getPath());
        lqw.eq(bo.getSize() != null, BlogResource::getSize, bo.getSize());
        lqw.eq(StringUtils.isNotBlank(bo.getMimeType()), BlogResource::getMimeType, bo.getMimeType());
        lqw.eq(bo.getStatus() != null, BlogResource::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreType()), BlogResource::getStoreType, bo.getStoreType());
        return lqw;
    }

    /**
     * 新增资源信息
     */
    @Override
    public Boolean insertByBo(BlogResourceBo bo) {
        BlogResource add = BeanUtil.toBean(bo, BlogResource.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改资源信息
     */
    @Override
    public Boolean updateByBo(BlogResourceBo bo) {
        BlogResource update = BeanUtil.toBean(bo, BlogResource.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BlogResource entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除资源信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
